import pandas as pd
import numpy as np
#%%
class utils():

    def __init__(self):
        pass

    #================================================================  
    #================ Initiatives and SDG names =====================
    #================================================================    
    def return_Initiatives(self):
        initiatives = ['communication', 'donation & funding', 'association', 'pricing',
                       'adoption of standards and rules', 'assessment and measurement',
                       'organizational structuring', 'modification of procedures',
                       'asset modification', 'training', 'incentives', 'r&d investments',
                       'new products', 'volunteerism']
        return initiatives
    def return_SDGLIST(self):
        return ['SDG '+str(k) for k in range(1, 18)] 
    def get_SpecificSDG(self, SDG):
        initiatives = self.return_Initiatives()
        return  [I+' - SDG '+str(SDG) for I in initiatives]      
    def get_SpecificInitiatives(self, initiative, SDGs):
        return  [initiative+' - SDG '+str(S) for S in SDGs]       
    def get_initiativeKeys(self, initiatives, SDGs):
        return list(np.array([[I+' - SDG '+str(S) for S in SDGs] for I in initiatives]).ravel())       

    def get_groupedInitiatives(self, Group):
        if Group == 'reactive':
            return(['communication', 'donation & funding', 'association', 'pricing'])
        elif Group == 'responsive':
            return(['adoption of standards and rules', 'assessment and measurement', 'organizational structuring', 'modification of procedures',   'asset modification', 'training'])
        elif Group == 'proactive':
            return(['incentives', 'r&d investments', 'new products', 'volunteerism'])
    def get_groupedInitiatives_cols(self, Group, SDGs):
        return list(np.array([[I+' - SDG '+str(S) for S in SDGs] for I in self.get_groupedInitiatives(Group)]).ravel())       
    def get_SpecificSDGsColumns(self, sdgs):
        return list(np.array([self.get_SpecificSDG(E) for E in sdgs]).ravel())
    def get_BehavioralKeys(self):
        '''
        Get all keys
        '''
        initiatives = self.return_Initiatives()
        behavioral_keys = list(np.ravel([[I+' - SDG '+str(n) for n in range(1, 18)] for I in initiatives]))
        return behavioral_keys


   

    #====================================================  
    #================ Miscellaneous =====================
    #====================================================  
    def make_mrg(self, X, date_type):
        try:
            mrg = X['gvkey'].astype(int).astype(str)+'-'+X[date_type].astype(int).astype(str)
        except Exception:
            mrg = X['gvkey'].astype(int).astype(str)+'-'+X[date_type].astype(str)
        return(mrg) 
    def inflation_series(self):
        '''
        Return the inflation series centered at the US dollar in 2000
        To convert a series into an inflation adjusted just divide by cpi_adj,
        e.g. sales adjusted = sales/cpi_adj
        '''
        i = pd.read_csv('../../Data/CPI.csv')
        i.columns = ['eom_date', 'cpi']
        i['reference'] = [float(i[i.eom_date == '2010-01-01'].cpi-100)]*len(i)
        i['cpi_adj'] = (i['cpi'] - i['reference'])/100
        return i
        
    

    def stdErr(self, X): return(X.dropna().std()/np.sqrt(len(X.dropna())))
    def stdErrBootstrapped(self, X,N=1000, func='np.median'):
        n  =  int(len(X)*2/3)
        r = []
        for i in range(N):
            tmp = np.random.choice(np.ravel(X.values).tolist(), n, replace = False)
            r.append(eval(func)(tmp))
        stdError = np.std(r)
        return(stdError)
    def significance(self, X): return(['***' if X < 0.01 else '**' if X < 0.05 else '*' if X < 0.1 else ''][0])


    
    
    